package me.ilvc.taotao.service.impl;

import com.github.pagehelper.PageHelper;

import java.util.Date;
import java.util.List;

import com.github.pagehelper.PageInfo;
import me.ilvc.taotao.dao.TbItemDao;
import me.ilvc.taotao.dao.TbItemDescDao;
import me.ilvc.taotao.model.TbItem;
import me.ilvc.taotao.model.TbItemDesc;
import me.ilvc.taotao.result.EasyUIResult;
import me.ilvc.taotao.service.ItemService;
import me.ilvc.taotao.utils.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: iLvc
 * @Date：Create in 10:50 2017/8/1
 * @Description:
 * @Modified By:
 */
@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private TbItemDao itemDao;
    @Autowired
    private TbItemDescDao itemDescDao;


    @Override
    public <T> T findById(Integer id) {
        return itemDao.findById(id);
    }

    @Override
    public <T> int insert(T t) {
        return 0;
    }

    @Override
    public <T> int update(T t) {
        return 0;
    }

    @Override
    public <T> int delete(T t) {
        return 0;
    }

    @Override
    public <T> T select(T t) {
        return null;
    }

    @Override
    public <T> List<T> findAll() {
        return this.itemDao.findAll();
    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public <T> int sizeByType(T t) {
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return 0;
    }

    @Override
    public int insert(TbItem record) {
        return 0;
    }

    @Override
    public int insertSelective(TbItem record) {
        return 0;
    }

    @Override
    public TbItem selectByPrimaryKey(Long id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(TbItem record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(TbItem record) {
        return 0;
    }

    @Override
    public EasyUIResult getItemList(Integer page,Integer rows) {
        PageHelper.startPage(page,rows);
        List<TbItem> list=this.itemDao.findAll();
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        EasyUIResult result = new EasyUIResult(total, list);
        return result;
    }

    @Override
    public int saveItem(TbItem item, String desc, String itemParams) {
        Date date = new Date();
        //获得商品id
        long id = IDUtils.genItemId();
        //添加商品信息
        item.setId(id);
        //商品状态，1-正常，2-下架，3-删除
        item.setStatus((byte) 1);
        item.setCreated(date);
        item.setUpdated(date);
        itemDao.insert(item);
        //添加商品描述
        //创建TbItemDesc对象
        TbItemDesc itemDesc = new TbItemDesc();
        //获得一个商品id
        itemDesc.setItemId(id);
        itemDesc.setItemDesc(desc);
        itemDesc.setCreated(date);
        itemDesc.setUpdated(date);
        //插入数据
        return itemDescDao.insert(itemDesc);
    }
}
